import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from global_params import *

class ConsoleTabs(QTabWidget):
    def __init__(self, parent = None):
        super(ConsoleTabs, self).__init__(parent)
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tab3 = QWidget()
          	
        self.addTab(self.tab1,"Tab 1")
        self.addTab(self.tab2,"Tab 2")
        #self.addTab(self.tab3,"Tab 3")
        self.tab1UI()
        self.tab2UI()
        #self.tab3UI()
        #self.setWindowTitle("tab demo")
         	
    def tab1UI(self):
        font = QFont()
        font.setFamily('Courier')
        font.setPointSize(9)
        layout = QHBoxLayout()
        self.logBox = QTextEdit()
        self.logBox.setFont(font)
        layout.addWidget(self.logBox)
        #layout.addRow("Address",QLineEdit())
        self.setTabText(0,"console")
        self.tab1.setLayout(layout)
         	
    def tab2UI(self):
       self.setTabText(1,"Reg_Watcher_Memory")
       layout = QHBoxLayout()

       regWidget = QWidget()
       regLayout = QFormLayout()
       self.PCText = QLineEdit()
       self.PCText.setReadOnly(1)
       self.SPText = QLineEdit()
       self.SPText.setReadOnly(1)
       self.RegAText = QLineEdit()
       self.RegAText.setReadOnly(1)
       self.RegXText = QLineEdit()
       self.RegXText.setReadOnly(1)
       self.RegYText = QLineEdit()
       self.RegYText.setReadOnly(1)
       regLayout.addRow("PC",self.PCText)
       regLayout.addRow("SP",self.SPText)
       regLayout.addRow("RegA",self.RegAText)
       regLayout.addRow("RegX",self.RegXText)
       regLayout.addRow("RegY",self.RegYText)
       regWidget.setLayout(regLayout)
       regWidget.setFixedWidth(80)

       flagWidget = QWidget()
       flagLayout = QFormLayout()
       self.FlagNText = QLineEdit("00")
       self.FlagNText.setReadOnly(1)
       self.FlagVText = QLineEdit()
       self.FlagVText.setReadOnly(1)
       self.FlagBText = QLineEdit()
       self.FlagBText.setReadOnly(1)
       self.FlagDText = QLineEdit()
       self.FlagDText.setReadOnly(1)
       self.FlagIText = QLineEdit()
       self.FlagIText.setReadOnly(1)
       self.FlagZText = QLineEdit()
       self.FlagZText.setReadOnly(1)
       self.FlagCText = QLineEdit()
       self.FlagCText.setReadOnly(1)
       flagLayout.addRow("FlagN",self.FlagNText)
       flagLayout.addRow("FlagV",self.FlagVText)
       flagLayout.addRow("FlagB",self.FlagBText)
       flagLayout.addRow("FlagD",self.FlagDText)
       flagLayout.addRow("FlagI",self.FlagIText)
       flagLayout.addRow("FlagZ",self.FlagZText)
       flagLayout.addRow("FlagC",self.FlagCText)
       flagWidget.setLayout(flagLayout)
       flagWidget.setFixedWidth(80)

       watchWidget = QWidget()
       watchLayout = QVBoxLayout()
       watcherTable = QTableWidget()
       watcherTable.setRowCount(16)
       watcherTable.setColumnCount(3)
       watcherTable.setColumnWidth(1,50)
       watcherTable.setColumnWidth(2,50)
       for row in range(16):
            watcherTable.setRowHeight(row,24)
            for col in range(3):
                watcherTable.setItem(row,col,QTableWidgetItem(""))

       watcherTable.setHorizontalHeaderLabels(("name,addr,value").split(","))
       watcherLabel = QLabel()
       watcherLabel.setText("Watcher")
       watchLayout.addWidget(watcherLabel)
       watchLayout.addWidget(watcherTable)
       watchWidget.setLayout(watchLayout)
       watchWidget.setFixedWidth(260)
       self.watcher = watcherTable


       VHeader = []
       HHeader = []
       for i in range(MEM_SIZE//0x20):
           VHeader.append("%03X"%(i*0x20))
       for i in range(0x20):
           HHeader.append("%02X"%(i))
       
       memTable = QTableWidget()
       memTable.setRowCount(MEM_SIZE//0x20)
       memTable.setColumnCount(32)
       memTable.setHorizontalHeaderLabels((";".join(HHeader)).split(";"))
       memTable.setVerticalHeaderLabels((";".join(VHeader)).split(";"))
       self.memTable = memTable
       for i in range(MEM_SIZE):
            row = i//0x20
            col = i%0x20
            memTable.setItem(row,col,QTableWidgetItem("00"))
       memTable.show()

       for col in range(0x20):
            memTable.setColumnWidth(col,24)

       #stackVHeader = []
       #stackHHeader = []
       #for i in range(16):
       #    stackVHeader.append("%03X"%(0x00 + i*0x20))
       #for i in range(0x20):
       #    stackHHeader.append("%02X"%(0x00 + i))
       #stackTable.resizeColumnsToContents()
       


       #for row in range(8):
       #     for col in range(0x20):
       #         stackTable.setItem(row,col,QTableWidgetItem("00"))
       #stackTable.show()
       #self.stackTable = stackTable


       #memWidget = QWidget()
       #memLayout = QHBoxLayout()
       #memLayout.addWidget(mem)
       ##memLayout.addWidget(stackTable)
       #memWidget.setLayout(memLayout)

       layout.addWidget(regWidget)
       layout.addWidget(flagWidget)
       layout.addWidget(watchWidget)
       layout.addWidget(memTable)
       #layout.addWidget(memWidget)

       layout.setAlignment(Qt.AlignTop)
       self.tab2.setLayout(layout)
         	
    #def tab3UI(self):
    #   self.setTabText(2,"Memory")
    #   layout = QHBoxLayout()

    #   VHeader = []
    #   HHeader = []
    #   for i in range(MEM_SIZE/0x20):
    #       VHeader.append("%03X"%(i*0x20))
    #   for i in range(0x20):
    #       HHeader.append("%02X"%(i))
    #   
    #   memTable = QTableWidget()
    #   memTable.setRowCount(MEM_SIZE/0x20)
    #   memTable.setColumnCount(32)
    #   memTable.setHorizontalHeaderLabels(QString(";".join(HHeader)).split(";"))
    #   memTable.setVerticalHeaderLabels(QString(";".join(VHeader)).split(";"))
    #   for i in range(MEM_SIZE):
    #        row = i/0x20
    #        col = i%0x20
    #        memTable.setItem(row,col,QTableWidgetItem("00"))
    #   memTable.show()

    #   self.memTable = memTable
    #   for col in range(0x20):
    #        memTable.setColumnWidth(col,24)

    #   layout.addWidget(memTable)
    #   self.tab3.setLayout(layout)

    def append(self,log):
        self.logBox.append(log)

    def setMem(self,mem):
        #print "memory size=",len(mem)
        self.mem = mem
        for i in range(len(mem)):
            row = i//0x20
            col = i%0x20
            cell = self.memTable.item(row,col)
            #if(i<100):
            #    print("raw cell(%d %d) = %s"%(row,col,cell.text()))
            #cell.setText("aa")
            #if(i<100):
            #    print("cell(%d %d) = %s"%(row,col,cell.text()))
            cell.setText("%02X"%mem[i])
            #if(i<0x600+100 and i>=0x600):
            #    print("cell(%d %d) = %s"%(row,col,cell.text()))
            #if(i<0x600+100 and i>=0x600):
            #    print("Addr[%04x]=%x"%(i,mem[i]))

    def loadWatchers(self,watchers):
        row = 0
        for k,v in watchers.items():
            cell_val = self.watcher.item(row,0)
            cell_addr = self.watcher.item(row,1)
            cell_val.setText((k))
            cell_addr.setText((v))
            row += 1

    def saveWatchers(self):
        row = 0
        watchers = {}
        #while True:
        val = self.watcher.item(row,0).text()
        addr = self.watcher.item(row,1).text()
        watchers.update({val:addr})
        return watchers

    def updateWatchers(self):
        for row in range(16):
            val = str(self.watcher.item(row,0).text())
            addr = str(self.watcher.item(row,1).text())
            #print "watcher ",val,addr
            if addr:
                try:
                    addr = int(addr,16)
                except:
                    print("the addr of %s is incorrect"%val)
                    return "the addr of %s is incorrect"%val
                if addr < MEM_SIZE:
                    cell = self.watcher.item(row,2)
                    cell.setText("%02X"%self.mem[addr])
                    #print "set %s = %02x"%(val,self.mem[addr])
        return ""


    #def dehighlight(self):
    #    self.memTable.clearSelection()
        #for i in range(MEM_SIZE):
        #    row = i//0x20
        #    col = i%0x20
        #    #pre_cell = QTableWidgetItem("00")
        #    cell = self.memTable.item(row,col)
        #    #cell.setItem
        #    #self.memTable.setItemSelected(pre_cell,0);
        #    #self.memTable.setItem(row,col,pre_cell)

    def highlight(self,i):
        self.memTable.setFocus()
        self.memTable.clearSelection()
        row = i//0x20
        col = i%0x20
        self.highlightCell = self.memTable.item(row,col)
        self.memTable.setRangeSelected(QTableWidgetSelectionRange(row, col, row, col), True)
        #self.memTable.item(row,col).setBackground(QColor(55,30,34))
        #cell = self.memTable.item(row,col)
        #self.memTable.scrollToItem(cell)
        #pre_cell = QTableWidgetItem("00")
        ##self.memTable.setItemSelected(cell,1);
        #self.memTable.setItem(row,col,pre_cell)

    def setPC(self,PC):
        self.PCText.setText("%02X"%PC) 

    def setSP(self,SP):
        self.SPText.setText("%02X"%SP)

    def setRegA(self,regA):
        self.RegAText.setText("%02X"%regA)

    def setRegX(self,regX):
        self.RegXText.setText("%02X"%regX)

    def setRegY(self,regY):
        self.RegYText.setText("%02X"%regY)

    def setFlagN(self,flagN):
        self.FlagNText.setText("%02X"%flagN)

    def setFlagV(self,flagV):
        self.FlagVText.setText("%02X"%flagV)

    def setFlagB(self,flagB):
        self.FlagBText.setText("%02X"%flagB)

    def setFlagD(self,flagD):
        self.FlagDText.setText("%02X"%flagD)

    def setFlagI(self,flagI):
        self.FlagIText.setText("%02X"%flagI)

    def setFlagZ(self,flagZ):
        self.FlagZText.setText("%02X"%flagZ)

    def setFlagC(self,flagC):
        self.FlagCText.setText("%02X"%flagC)
		
def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())
	
if __name__ == '__main__':
   main()
